﻿using System.Collections.Generic;
using System.Text;

namespace Borg.AttributeAnalyze.Result
{
    public class InvalidHandlerParameter : MissingHandler
    {
        private readonly IEnumerable<HandlerParameterInfo> _missmatchInfo;

        public InvalidHandlerParameter(ExtensionTypeInfo extension, string handler,
            string implementingMethod, IEnumerable<HandlerParameterInfo> missmatchInfo)
            : base(extension, handler, implementingMethod)
        {
            _missmatchInfo = missmatchInfo;
            Result = ResultType.InvalidHandlerParameter;
        }

        public InvalidHandlerParameter(PlugTypeInfo plug, string handler,
            string implementingMethod, IEnumerable<HandlerParameterInfo> missmatchInfo)
            : base(plug, handler, implementingMethod)
        {
            _missmatchInfo = missmatchInfo;
            Result = ResultType.InvalidHandlerParameter;
        }

        public InvalidHandlerParameter(SlotTypeInfo slot, string handler,
            string implementingMethod, IEnumerable<HandlerParameterInfo> missmatchInfo)
            : base(slot, handler, implementingMethod)
        {
            _missmatchInfo = missmatchInfo;
            Result = ResultType.InvalidHandlerParameter;
        }

        public override string ToMsBuildMessageString()
        {
            var builder = new StringBuilder();

            builder.AppendFormat(
                "invalid handler parameter\n{0}method: {1}\n{0}handler: {2}\n{0}class: {3}\n",
                new object[] { GetWhiteSpace(2), ImplementingMethod, Handler, TypeName });

            foreach (var parameter in _missmatchInfo)
                builder.AppendFormat(
                    "{0}parameter position: {1}\n{0}required parameter type: {2}\n{0}specified parameter type: {3}\n",
                    new object[] {GetWhiteSpace(4), parameter.Position, parameter.SpecifiedType, parameter.ProvidedType});

            return builder.ToString().Trim();
        }

        public new string ImplementingMethod
        {
            get { return base.ImplementingMethod; }
        }

        public override MessageType MessageType
        {
            get { return MessageType.Error; }
        }
    }
}

